//
//  CLVCBookSheif.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/5/7.
//

#import "CLVCBookSheif.h"
#import <ChuckTableView/ChuckCollectionView.h>
#import "CLBook.h"
#import "CLCellBookSheif.h"
#import "CLVCMain.h"
#import "CLExtentionsManager.h"
#import "CLVCNovelInfo.h"
#import "CLHTTPURLConfiguration.h"
#import <Flutter/Flutter.h>
@interface CLVCBookSheif () <ChuckDelegate>
@property (nonatomic, strong) ChuckCollectionView *collectionView;
@property (nonatomic, strong) NSArray *books;
@end

@implementation CLVCBookSheif

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubViews];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self readCache];
}

- (void)configSubViews {
    if (@available(iOS 13.0, *)) {
        UIColor *backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            return [traitCollection userInterfaceStyle] == UIUserInterfaceStyleDark ? UIColorFromRGB(0x262626) : UIColorFromRGB(0xE9E9E9);
        }];
        self.view.backgroundColor = backgroundColor;
    }
    NSInteger clum = 3;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 23;
    layout.minimumLineSpacing = 12;
    layout.sectionInset = UIEdgeInsetsMake(11, 22, 11, 22);
    CGFloat width = (SCREENWIDTH - layout.minimumInteritemSpacing * (clum - 1) - layout.sectionInset.left - layout.sectionInset.right)/(CGFloat)clum;
    CGFloat height = width * 115/98.0;
    layout.itemSize = CGSizeMake(width, height+4+35);
    
    self.collectionView = [[ChuckCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout vcDelegate:self configureBlock:^(CLCellBookSheif *cell, id model, NSIndexPath *indexPath) {
    } cellDidselectConfig:^(id cell, CLBook *book, NSIndexPath *indexPath) {
        NSString *url = [CLHTTPURLConfiguration.sharedInstance urlSearchBookInfoWithTitle:book.title];
        book.url = url;
        NSLog(@"跳转网页:%@ - %p,%@ %@",book.title,book,book.url,book.bookid);
//        UIViewController *vc = [[FlutterViewController alloc]initWithEngine:CLSharedAppDelegate.engine nibName:nil bundle:nil];
        CLVCNovelInfo *vc = [[CLVCNovelInfo alloc] initWithBook:book];
        [CLNavigationService pushController:vc animated:YES];
    }];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor clearColor];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }
}

- (void)readCache {
    NSArray *books = [[CLReadParser sharedInstance] books];
    [self.collectionView clearTableViewCell];
    [self.collectionView addModels:books cellClass:[CLCellBookSheif class] section:0];
    [self.collectionView reloadData];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 13.0, *)) {
        UIColor *backgroundColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            return [traitCollection userInterfaceStyle] == UIUserInterfaceStyleDark ? UIColorFromRGB(0x262626) : UIColorFromRGB(0xE9E9E9);
        }];
        self.view.backgroundColor = backgroundColor;
    }
}
@end
